package com.tang.exception;

import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 全局异常处理
 *
 * @author cfpu003
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleError(Exception ex) {
        if (ex instanceof HttpRequestMethodNotSupportedException) {
            return new ResponseEntity<>("Method Not Allowed",
                    HttpStatus.METHOD_NOT_ALLOWED);
        } else if (ex instanceof HttpMediaTypeNotSupportedException) {
            return new ResponseEntity<>("Unsupported Media Type",
                    HttpStatus.UNSUPPORTED_MEDIA_TYPE);
        } else if (ex instanceof HttpMediaTypeNotAcceptableException) {
            return new ResponseEntity<>("Not Acceptable",
                    HttpStatus.NOT_ACCEPTABLE);
        } else if (ex instanceof MissingPathVariableException) {
            return new ResponseEntity<>("Bad Request", HttpStatus.BAD_REQUEST);
        } else if (ex instanceof MissingServletRequestParameterException) {
            return new ResponseEntity<>("Bad Request", HttpStatus.BAD_REQUEST);
        } else if (ex instanceof ServletRequestBindingException) {
            return new ResponseEntity<>("Bad Request", HttpStatus.BAD_REQUEST);
        } else if (ex instanceof ConversionNotSupportedException) {
            return new ResponseEntity<>("Internal Server Error",
                    HttpStatus.INTERNAL_SERVER_ERROR);
        } else if (ex instanceof TypeMismatchException) {
            return new ResponseEntity<>("Bad Request", HttpStatus.BAD_REQUEST);
        } else if (ex instanceof HttpMessageNotReadableException) {
            return new ResponseEntity<>("Bad Request", HttpStatus.BAD_REQUEST);
        } else if (ex instanceof HttpMessageNotWritableException) {
            return new ResponseEntity<>("Internal Server Error",
                    HttpStatus.INTERNAL_SERVER_ERROR);
        } else if (ex instanceof MethodArgumentNotValidException) {
            return new ResponseEntity<>("Bad Request", HttpStatus.BAD_REQUEST);
        } else if (ex instanceof MissingServletRequestPartException) {
            return new ResponseEntity<>("Bad Request", HttpStatus.BAD_REQUEST);
        } else if (ex instanceof BindException) {
            return new ResponseEntity<>("Bad Request", HttpStatus.BAD_REQUEST);
        } else if (ex instanceof NoHandlerFoundException) {
            return new ResponseEntity<>("Not Found", HttpStatus.NOT_FOUND);
        } else if (ex instanceof AsyncRequestTimeoutException) {
            return new ResponseEntity<>("Service Unavailable",
                    HttpStatus.SERVICE_UNAVAILABLE);
        } else {
            return new ResponseEntity<>("Internal Server Error",
                    HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }

}
